<template>
  <div v-if="show">
    <el-dialog :visible.sync="dialogVisible" fullscreen append-to-body>
      <el-container>
        <el-main style="height:calc(100vh  - 160px); padding: 0 45px;">
          <div class="score">
            <el-button type="primary" @click="exportExcel(cepingHzglb.hzName)">导出统计表</el-button>
            <el-button type="primary" @click="viewMxAll(0)">综合得分明细</el-button>
          </div>
          <div class="customTableHeader">
            <div class="customTableTitle">{{ cepingHzglb.hzName }}</div>
          </div>
          <div class="cpTable">
            <el-table ref='cpphztj-table' :data="cepingHzglDxpzbList" border stripe height="calc(100vh - 287px)">
              <el-table-column label="序号" width="100" type="index">
              </el-table-column>
              <el-table-column prop="deptName" label="部门" min-width="200" header-align="center" align="center">
                <template slot-scope="scope">
                  <span v-if="cepingHzglb.hzDx=='个人'">
                    {{ scope.row.deptName }}
                  </span>
                  <span v-else>
                    {{ scope.row.deptName }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="userName" label="姓名" min-width="200" header-align="center" align="center"
                               v-if="cepingHzglb.hzDx=='个人'">
                <template slot-scope="scope">
                  <span>
                    {{ scope.row.userName }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="userZw" label="职务" min-width="200" header-align="center" align="center"
                               v-if="cepingHzglb.hzDx=='个人'">
              </el-table-column>
              <el-table-column v-for="(item,index) in cList" :key="index" :prop="item"
                               :label="getcpName(item)" min-width="150" header-align="center" align="center">
              </el-table-column>
              <el-table-column prop="df" label="综合得分" min-width="150" header-align="center" align="center">
                <template slot-scope="scope">
                  <!--                  <span style="color: #3e51a3;cursor: pointer;" @click="viewMx(scope.row)">-->
                  <!--                    {{ scope.row.df }}-->
                  <!--                  </span>-->
                  <span>
                    {{ keepTwoDecimalFull(scope.row.df,2) }}
                  </span>
                </template>
              </el-table-column>
              <el-table-column prop="jg" label="测评结果" min-width="150" header-align="center" align="center"
                               v-if="pmShow">
              </el-table-column>
            </el-table>
          </div>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer flex justifyContentSpaceBetween">
        <div class="flex alignItemsCenter">
          <div class="font16">
            排名设置：<el-checkbox v-model="pmShow">显示排名</el-checkbox>
          </div>
          <div v-if="pmShow" style="margin:0 10px">
            <el-button type="primary" @click="szDialogVisible = true">排名设置</el-button>
          </div>
          <div v-if="cepingHzglb.hzFs == '公式' && cepingHzglb.hzGssm " style="margin:0 10px">
            综合得分 = {{ cepingHzglb.hzGssm }}
          </div>
        </div>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="排名设置" :visible.sync="szDialogVisible" width="370px" append-to-body>
      <el-container>
        <el-main style="max-height:60vh">
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[0]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[0]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[0] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[1]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[1]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[1] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[2]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[2]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[2] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[3]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[3]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[3] > 100" class="red">* 输入的范围小于100%</div>
          </div>
          <div style="margin-bottom:20px;margin-left: 30px;">
            <el-input v-model="pmName[4]" style="width: 100px;" :disabled="true"></el-input>
            <el-input-number v-model="pm[4]" :min="0" class="ml10"></el-input-number>
            %
            <div v-show="pm[4] > 100" class="red">* 输入的范围小于100%</div>
          </div>
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="pmQr">确定设置</el-button>
        <el-button @click="szDialogVisible = false">关闭</el-button>
       
      </span>
    </el-dialog>
    <el-dialog title="明细查询" :visible.sync="mxDialogVisible" :width="getDjmx()?'1800px':'1200px'" append-to-body>
      <div style="margin-bottom:20px">
        <el-table ref="zbABTable" :data="mxList" border stripe height="400px">
          <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpCode" label="票种" min-width="60" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCode }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpCsl" label="总票数" min-width="80" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpCsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpDsl" label="提交票数" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpDsl }}票
            </template>
          </el-table-column>
          <el-table-column prop="cpQz" label="票种权重" min-width="100" header-align="center" align="center">
            <template slot-scope="scope">
              {{ scope.row.cpQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjf" label="平均分数" min-width="120" header-align="center" align="center">
          </el-table-column>
          <el-table-column prop="cpqz" label="测评项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.cpqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfCph" label="测评项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="dcqz" label="等次项权重" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
            <template slot-scope="scope">
              {{ scope.row.dcqz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cpQzpjfDch" label="等次项分数" min-width="120" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cpQzpjfZzfs" label="测评分数" min-width="100" header-align="center" align="center"
                           v-if="getDjmx()">
          </el-table-column>
          <el-table-column prop="cppQz" label="测评票权重" min-width="120" header-align="center" align="center"
                           v-if="getCppQz()">
            <template slot-scope="scope">
              {{ scope.row.cppQz }}%
            </template>
          </el-table-column>
          <el-table-column prop="cppFz" label="最终分数" min-width="100" header-align="center" align="center"
                           v-if="getCppQz()">
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exportExcelAB">导出</el-button>
        <el-button @click="mxDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="xqDialogVisible" v-if="xqDialogVisible" fullscreen append-to-body>
      <div v-if="mxLx=='pj'">
        <div class="score">
          <el-button type="primary" @click="exportExcelXq">导出</el-button>
        </div>
        <div class="customTableHeader">
          <div class="customTableTitle">{{ cepingHzglb.hzName }}</div>
        </div>
        <div style="margin-bottom:20px">
          <el-table ref="xqABTable" :data="tableData" border stripe height="calc(100vh - 247px)">
            <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="hdName" label="被测评" min-width="150" header-align="center" align="center"
                             :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column :prop="'hj_'+t1.id" :label="t1.zbMc + (t1.zbQz>0?('('+t1.zbQz+'%)'):'')" min-width="175"
                             header-align="center" align="center"
                             v-for="(t1,i1) in cepingXmmxbListTree" :key="i1*10000">
              <el-table-column :prop="'hj_'+t2.id" :label="t2.zbMc + (t2.zbQz>0?('('+t2.zbQz+'%)'):'')" min-width="175"
                               header-align="center"
                               align="center" v-if="t1.children" v-for="(t2,i2) in t1.children"
                               :key="i1*10000+i2*100">

                <el-table-column :prop="'hj_'+t3.id" :label="t3.zbMc + (t3.zbQz>0?('('+t3.zbQz+'%)'):'')"
                                 min-width="175"
                                 header-align="center"
                                 align="center" v-if="t2.children" v-for="(t3,i3) in t2.children"
                                 :key="i1*10000+i2*100+i3">

                                 <el-table-column label="得分" min-width="100" header-align="center" align="center">
                                  <template slot-scope="scope">
                                    {{ scope.row['hj_' + t3.id] }}
                                  </template>
                                </el-table-column>
                                <el-table-column :label="getcpName(x1)" min-width="100" header-align="center" align="center"
                                                v-for="(x1,j1) in cList" :key="j1">
                                  <template slot-scope="scope">
                                    {{ scope.row[x1 + "_" + t3.id + "_pj"] }} 
                                  </template>
                                </el-table-column>
                </el-table-column>
                <el-table-column v-if="!t2.children" label="得分" min-width="100" header-align="center" align="center">
                  <template slot-scope="scope">
                    {{ scope.row['hj_' + t2.id] }}
                  </template>
                </el-table-column>
                <el-table-column v-if="!t2.children" :label="getcpName(x1)" min-width="100" header-align="center" align="center"
                                 v-for="(x1,j1) in cList" :key="j1">
                  <template slot-scope="scope">
                    {{ scope.row[x1 + "_" + t2.id + "_pj"] }} 
                  </template>
                </el-table-column>
              </el-table-column>

              <el-table-column v-if="!t1.children" label="得分" min-width="100" header-align="center" align="center">
                  <template slot-scope="scope">
                    {{ scope.row['hj_' + t1.id] }}  
                  </template>
                </el-table-column>
                <el-table-column v-if="!t1.children" :label="getcpName(x1)" min-width="100" header-align="center" align="center"
                                 v-for="(x1,j1) in cList" :key="j1">
                  <template slot-scope="scope">
                    {{ scope.row[x1 + "_" + t1.id + "_pj"] }} 
                  </template>
                </el-table-column>
            </el-table-column>
            <el-table-column prop="cpFz" label="指标汇总" min-width="150" header-align="center" align="center">
              <template slot-scope="scope">
                {{ keepTwoDecimalFull(scope.row.cpFz, 2) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div v-if="mxLx=='dfmx'">
        <div class="score">
          <el-button type="primary" @click="exportExcelXq">导出</el-button>
        </div>
        <div class="customTableHeader">
          <div class="customTableTitle">{{ cepingHzglb.hzName }}</div>
        </div>
        <div style="margin-bottom:20px">
          <el-table ref="xqABTable" :data="tableData" border stripe height="calc(100vh - 247px)">
            <el-table-column label="序号" width="100" type="index" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="hdName" label="被测评" min-width="150" header-align="center" align="center"
                             :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column :prop="'hj_'+t1.id" :label="t1.zbMc + (t1.zbQz>0?('('+t1.zbQz+'%)'):'')" min-width="175"
                             header-align="center" align="center"
                             v-for="(t1,i1) in cepingXmmxbListTree" :key="i1*10000">
              <el-table-column :prop="'hj_'+t2.id" :label="t2.zbMc + (t2.zbQz>0?('('+t2.zbQz+'%)'):'')" min-width="175"
                               header-align="center"
                               align="center" v-if="t1.children" v-for="(t2,i2) in t1.children"
                               :key="i1*10000+i2*100">
                <el-table-column :prop="'hj_'+t3.id" :label="t3.zbMc + (t3.zbQz>0?('('+t3.zbQz+'%)'):'')"
                                 min-width="175"
                                 header-align="center"
                                 align="center" v-if="t2.children" v-for="(t3,i3) in t2.children"
                                 :key="i1*10000+i2*100+i3">
                </el-table-column>
                <el-table-column label="得分" min-width="100" header-align="center" align="center">
                  <template slot-scope="scope">
                    {{ scope.row['hj_' + t2.id] }}
                  </template>
                </el-table-column>
                <el-table-column :label="x1+'票'" min-width="100" header-align="center" align="center"
                                 v-for="(x1,j1) in cList" :key="j1">
                  <el-table-column :label="'低于'+(xyfz/10)+'分票'" min-width="110" header-align="center" align="center">
                    <template slot-scope="scope">
                    <span v-if="scope.row[x1 + '_' + t2.id + '_df']">
                    {{ scope.row[x1 + "_" + t2.id + "_df"] }}
                    
                    </span>
                      <span v-else>
                      0
                    </span>
                    </template>
                  </el-table-column>
                  <el-table-column label="占比" min-width="80" header-align="center" align="center">
                    <template slot-scope="scope">
                    <span>{{
                        !scope.row[x1 + "_" + t2.id + "_df"] || scope.row[x1 + "_" + t2.id + "_df"] == 0 ? 0 : Math.round(scope.row[x1 + "_" + t2.id + "_df"] / scope.row[x1+"_dfl"] * 10000) / 100
                      }} %
                    </span>
                    </template>
                  </el-table-column>
                </el-table-column>
              </el-table-column>
            </el-table-column>
            <el-table-column prop="cpFz" label="指标汇总" min-width="150" header-align="center" align="center">
              <template slot-scope="scope">
                {{ keepTwoDecimalFull(scope.row.cpFz, 2) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div v-if="mxLx=='dfhz'">
        <div class="score">
          <el-button type="primary" @click="exportExcelXq">导出</el-button>
        </div>
        <div class="customTableHeader">
          <div class="customTableTitle">{{ cepingHzglb.hzName }}</div>
        </div>
        <div style="margin-bottom:20px">
          <el-table ref="xqABTable" :data="cepingHzglDxpzbList" border stripe height="calc(100vh - 247px)">
            <el-table-column label="序号" width="100" type="index">
            </el-table-column>
            <el-table-column prop="deptName" label="部门" min-width="200" header-align="center" align="center">
              <template slot-scope="scope">
                  <span v-if="cepingHzglb.hzDx=='个人'">
                    {{ scope.row.deptName }}
                  </span>
                <span v-else>
                    {{ scope.row.deptName }}
                  </span>
              </template>
            </el-table-column>
            <el-table-column prop="userName" label="姓名" min-width="200" header-align="center" align="center"
                             v-if="cepingHzglb.hzDx=='个人'">
              <template slot-scope="scope">
                  <span>
                    {{ scope.row.userName }}
                  </span>
              </template>
            </el-table-column>
            <el-table-column prop="userZw" label="职务" min-width="200" header-align="center" align="center"
                             v-if="cepingHzglb.hzDx=='个人'">
            </el-table-column>
            <el-table-column v-for="(item,index) in cList" :key="index" :prop="item"
                             :label="getcpName(item)" min-width="150" header-align="center" align="center">
            </el-table-column>
            <el-table-column prop="df" label="综合得分" min-width="150" header-align="center" align="center">
              <template slot-scope="scope">
              <span>
                {{ keepTwoDecimalFull(scope.row.df, 2) }}
<!--                {{ scope.row.df }}-->
              </span>
              </template>
            </el-table-column>
            <el-table-column :label="'低于'+(xyfz/10)+'分票情况说明'" min-width="250" header-align="center" align="left">
              <template slot-scope="scope">
                <span v-html="getDfsm(scope.row)"></span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer flex justifyContentSpaceBetween">
        <div class="flex alignItemsCenter">
          <div style="margin:0 10px">
            <el-button :type="mxLx == 'pj'?'primary':''" @click="setMxLx('pj')">测评平均成绩</el-button>
            <el-button :type="mxLx == 'dfmx'?'primary':''" @click="setMxLx('dfmx')">测评低于{{ xyfz / 10 }}分明细</el-button>
            <el-button :type="mxLx == 'dfhz'?'primary':''" @click="setMxLx('dfhz')">测评低于{{ xyfz / 10 }}分汇总</el-button>
          </div>
          <div class="font16" v-if="mxLx == 'dfmx'||mxLx == 'dfhz'">
            低分设置：<el-select v-model="xyfz" placeholder="请选择" @change="selectDf">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
          </div>
        </div>
        <el-button type="primary" plain @click="xqDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {hzInfo, hzInfo4P1, putObj} from '@/api/ceping/cepinghzglb'
import {hzInfo as hdhz, hzInfos} from '@/api/ceping/cepinghdglhdzhhzb'
import {bcpTjInfo} from '@/api/ceping/cepinghdglhdcphzb'
import {getBcpDfList, getBcpDfLists} from '@/api/ceping/cepinghdglxpxxdfjlbfb'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  name: "cepinghztjDialog",
  data() {
    return {
      show: false,
      dialogVisible: false,
      szDialogVisible: false,
      mxDialogVisible: false,
      xqDialogVisible: false,
      cepingHzglb: {},
      cepingHzglDxpzbList: [],
      cepingHzglHdfwbList: [],
      jlts: 0,
      pm: [20, 20, 20, 20, 20],
      pmName: ["A", "B", "C", "D", "E"],
      pmShow: true,
      mxList: [],
      cepingHdglCppmxbInfo: {},
      codeNameJson:{},
      cepingXmmxbListTree: [],
      xmxxXmmx: [],
      tableData: [],//指标结构
      tableData1:[],
      cList: [],
      mxLx: "pj",
      xyfz: 50,
      options: [{
        label: '小于10分',
        value: 100
      }, {
        label: '小于9分',
        value: 90
      }, {
        label: '小于8分',
        value: 80
      }, {
        label: '小于7分',
        value: 70
      }, {
        label: '小于6分',
        value: 60
      }, {
        label: '小于5分',
        value: 50
      }, {
        label: '小于4分',
        value: 40
      }, {
        label: '小于3分',
        value: 30
      }, {
        label: '小于2分',
        value: 20
      }, {
        label: '小于1分',
        value: 10
      }],
      loading: null,
      hdInfos: {},
      dfInfos: {}
    }
  },
  methods: {
    getcpName(cpCode){
      let tmp = this.codeNameJson[cpCode]
      let name = tmp ? tmp : cpCode
      return name
    },
    setMxLx(t) {
      this.mxLx = "";
      
      this.$nextTick(() => {
        this.mxLx = t;
        if(t == 'dfmx'){
          this.selectDf()
        }
      })
    },
    init(id) {
      const loading = this.$loading({
        lock: true,
        text: '正在汇总计算，请销后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      this.show = false;
      this.pm = [20, 20, 20, 20, 20];
      this.pmName = ["A", "B", "C", "D", "E"];
      this.pmShow = true;
      console.log(id)
      hzInfo4P1(id).then(res => {
        //console.log(res.data.data)
        if(res.data.data===-1){
          this.$notify.warning(res.data.msg);
          loading.close();
          return false;
        }
        this.cepingHzglb = res.data.data.cepingHzglb;
        if (this.cepingHzglb.hzJgbl) {
          this.pm = JSON.parse(this.cepingHzglb.hzJgbl).pm;
          this.pmName = JSON.parse(this.cepingHzglb.hzJgbl).pmName;
        } else {
          this.pm = [20, 20, 20, 20, 20];
          this.pmName = ["A", "B", "C", "D", "E"];
        }
        this.cepingHzglHdfwbList = res.data.data.cepingHzglHdfwbList;
        function compareZb(a, b) {
          return b.df - a.df;
        }
        this.cepingHzglDxpzbList = res.data.data.hzRestlt.sort(compareZb);
        this.jlts = this.cepingHzglDxpzbList.length;

        this.cList = res.data.data.cList.sort();
        this.codeNameJson = res.data.data.codeNameMap
        loading.close();
        this.show = true;
        this.dialogVisible = true;

        // let cList = [];
        // let x = 0;
        // for (let i in this.cepingHzglDxpzbList) {
        //   let row = this.cepingHzglDxpzbList[i];
        //   let mxList = [];
        //   bcpTjInfo(this.cepingHzglb.id, this.cepingHzglb.hzDx == "部门" ? row.deptId : (row.userLx == "班子" ? row.deptId : row.userId), this.cepingHzglb.hzDx == "部门" ? 2 : (row.userLx == "班子" ? 2 : 1)).then(res => {
        //     mxList = this.shujuHandle(res.data.data);
        //     for (var y in mxList) {
        //       if (cList.indexOf(mxList[y].cpCode) == -1) {
        //         cList.push(mxList[y].cpCode)
        //       }
        //       row[mxList[y].cpCode] = mxList[y].cppFz;
        //     }
        //     x++;
        //     if (x == this.jlts) {
        //      //console.log(this.cepingHzglDxpzbList)
        //       this.cList = cList;
        //       loading.close();
        //       this.show = true;
        //       this.dialogVisible = true;
        //     }
        //   });
        // }
      })
    },
    pmQr() {
      let num = 0;
      for (var i in this.pm) {
        if (this.pm[i] == null) {
          this.pm[i] = 0;
          this.$set(this.pm, i, 0)
        }
        num += this.pm[i]
      }
      if (num.toFixed(2) != 100) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名比率总和为“" + num + "%”，不是100%，无法这样应用");
        return;
      }
      if (this.pm[1] > 0 && this.pm[0] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[1] + "为“" + this.pm[1] + "%”，但A为“0%”，无法这样应用");
        return;
      }
      if (this.pm[2] > 0 && this.pm[1] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[2] + "为“" + this.pm[2] + "%”，但B为“0%”，无法这样应用");
        return;
      }
      if (this.pm[3] > 0 && this.pm[2] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[3] + "为“" + this.pm[3] + "%”，但C为“0%”，无法这样应用");
        return;
      }
      if (this.pm[4] > 0 && this.pm[3] == 0) {
        this.$notify.closeAll();
        this.$notify.error("您设置的排名" + this.pmName[4] + "为“" + this.pm[4] + "%”，但D为“0%”，无法这样应用");
        return;
      }
      let data = {
        pm: this.pm,
        pmName: this.pmName
      }
      let info = {
        id: this.cepingHzglb.id,
        hzJgbl: JSON.stringify(data)
      }
      putObj(info).then(res => {
        this.szDialogVisible = false;
        this.init(this.cepingHzglb.id)
      })
    },
    /**
     * excel 导出
     */
    exportExcel(excelName) {
      try {
        const $e = this.$refs['cpphztj-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    },
    showMx(row) {
      this.mxList = [];
      bcpTjInfo(this.cepingHzglb.id, this.cepingHzglb.hzDx == "部门" ? row.deptId : (row.bcpDx == "班子" ? row.deptId : row.userId), this.cepingHzglb.hzDx == "部门" ? 2 : (row.bcpDx == "班子" ? 2 : 1)).then(res => {
        this.mxList = this.shujuHandle(res.data.data);
        this.mxDialogVisible = true;
      });
    },
    shujuHandle(list) {
      //console.log(list)
      var data = [];
      var datajg = [];

      for (var a in list) {
        if (data.length > 0) {
          var bz = true;
          for (var b in data) {
            if (data[b].cpCode == list[a].cpCode) {
              data[b].cpCsl = data[b].cpCsl + list[a].cpCsl;
              data[b].cpDsl = data[b].cpDsl + list[a].cpDsl;
              data[b].cpPjf = data[b].cpPjf + list[a].cpPjf;
              data[b].cpQz = data[b].cpQz + list[a].cpQz;
              data[b].cpQzpjf = data[b].cpQzpjf + list[a].cpQzpjf;
              data[b].cpQzpjfCph = data[b].cpQzpjfCph + parseFloat(list[a].cpQzpjfCph);
              data[b].cpQzpjfDch = data[b].cpQzpjfDch + parseFloat(list[a].cpQzpjfDch);
              data[b].cpQzpjfZzfs = data[b].cpQzpjfZzfs + parseFloat(list[a].cpQzpjfZzfs);
              data[b].cpWsl = data[b].cpWsl + list[a].cpWsl;
              data[b].cppFz = data[b].cppFz + parseFloat(list[a].cppFz);
              data[b].cppQz = data[b].cppQz + list[a].cppQz;
              data[b].cpqz = data[b].cpqz + list[a].cpqz;
              data[b].dcqz = data[b].dcqz + list[a].dcqz;
              data[b].cs = data[b].cs + 1;

              bz = false;
            }
          }

          if (bz) {
            data.push({
              cpCode: list[a].cpCode,
              cpCsl: list[a].cpCsl,
              cpDsl: list[a].cpDsl,
              cpPjf: list[a].cpPjf,
              cpQz: list[a].cpQz,
              cpQzpjf: list[a].cpQzpjf,
              cpQzpjfCph: parseFloat(list[a].cpQzpjfCph),
              cpQzpjfDch: parseFloat(list[a].cpQzpjfDch),
              cpQzpjfZzfs: parseFloat(list[a].cpQzpjfZzfs),
              cpWsl: list[a].cpWsl,
              cppFz: parseFloat(list[a].cppFz),
              cppQz: list[a].cppQz,
              cpqz: list[a].cpqz,
              dcqz: list[a].dcqz,
              cs: 1,
            });
          }
        } else {
          data.push({
            cpCode: list[a].cpCode,
            cpCsl: list[a].cpCsl,
            cpDsl: list[a].cpDsl,
            cpPjf: list[a].cpPjf,
            cpQz: list[a].cpQz,
            cpQzpjf: list[a].cpQzpjf,
            cpQzpjfCph: parseFloat(list[a].cpQzpjfCph),
            cpQzpjfDch: parseFloat(list[a].cpQzpjfDch),
            cpQzpjfZzfs: parseFloat(list[a].cpQzpjfZzfs),
            cpWsl: list[a].cpWsl,
            cppFz: parseFloat(list[a].cppFz),
            cppQz: list[a].cppQz,
            cpqz: list[a].cpqz,
            dcqz: list[a].dcqz,
            cs: 1,
          });
        }
      }

      for (var c in data) {
        datajg.push({
          cpCode: data[c].cpCode,
          cpCsl: data[c].cpCsl,
          cpDsl: data[c].cpDsl,
          cpPjf: parseFloat((data[c].cpPjf / data[c].cs).toFixed(2)),
          cpQz: parseFloat((data[c].cpQz / data[c].cs).toFixed(2)),
          cpQzpjf: parseFloat((data[c].cpQzpjf / data[c].cs).toFixed(2)),
          cpQzpjfCph: parseFloat((data[c].cpQzpjfCph / data[c].cs).toFixed(2)),
          cpQzpjfDch: parseFloat((data[c].cpQzpjfDch / data[c].cs).toFixed(2)),
          cpQzpjfZzfs: parseFloat((data[c].cpQzpjfZzfs / data[c].cs).toFixed(2)),
          cpWsl: data[c].cpWsl,
          cppFz: parseFloat((data[c].cppFz / data[c].cs).toFixed(2)),
          cppQz: parseFloat((data[c].cppQz / data[c].cs).toFixed(2)),
          cpqz: parseFloat((data[c].cpqz / data[c].cs).toFixed(2)),
          dcqz: parseFloat((data[c].dcqz / data[c].cs).toFixed(2))
        });
      }

      return datajg;
    },
    viewMxAll(a) {
      this.loading = this.$loading({
        lock: true,
        text: '正在汇总计算明细，请销后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let hdidsAll = [];
      for (var i in this.cepingHzglHdfwbList) {
        if (hdidsAll.indexOf(this.cepingHzglHdfwbList[i].hdId) == -1) {
          hdidsAll.push(this.cepingHzglHdfwbList[i].hdId)
        }
      }
    //  console.log("点击综合得分明细:"+JSON.stringify(this.cepingHzglHdfwbList))
      hzInfos(hdidsAll.join()).then(res => {
        this.hdInfos = res.data.data;
        this.viewMxAlls(0);

      })
    },
    getHdhz(id) {
      let res = {
        data: {
          data: this.hdInfos[id]
        }
      }
      return res;
    },
    viewMxAlls(a) {
      if (a == 0) {
        this.tableData = [];
      }
      this.loading.text = '正在汇总计算明细（' + (a + 1) + '/' + (this.cepingHzglDxpzbList.length) + '），请销后...';
      let row = this.cepingHzglDxpzbList[a];
      let tableData = [];
      let hdids = [],cppIds=[];
      for (var i in this.cepingHzglHdfwbList) {
        ////console.log("this.cepingHzglHdfwbList:"+JSON.stringify(this.cepingHzglHdfwbList))
       // console.log("row:"+JSON.stringify(row))
        if (row["hd_" + this.cepingHzglHdfwbList[i].hdId]) {
          hdids.push(this.cepingHzglHdfwbList[i].hdId)
          cppIds.push(this.cepingHzglHdfwbList[i].hdHzmc)
        }
      }

      let j = 0;

      for (let xi in hdids) {
        let res = this.getHdhz(hdids[xi]);

        j++;
        let cepingHdglb = res.data.data.cepingHdglb;
        let cepingHdglCppmxbList = res.data.data.cepingHdglCppmxbList;
        let cepingHdglHdcphzbList = res.data.data.cepingHdglHdcphzbList;
        //this.cepingXmmxbList = res.data.data.cepingXmmxbList;
        let cepingHdglHdzhhzbList = res.data.data.cepingHdglHdzhhzbList;
        let cepingHdglHdhzmxbList = res.data.data.cepingHdglHdhzmxbList;
        //console.log("初始:"+JSON.stringify(cepingHdglCppmxbList))
        //填表
        let dataObj = {};
        let data = cepingHdglHdzhhzbList;
        for (var i in data) {
          dataObj[(data[i].bcpDx == '部门' ? "dept" : "user") + "_" + (data[i].bcpDx == '部门' ? data[i].bcpDeptId : data[i].bcpUserId)] = data[i];
        }

        for (var i in cepingHdglHdcphzbList) {
          var info = cepingHdglHdcphzbList[i];
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpFz"] = info.cpFz;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpDj"] = info.cpDj;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpZf"] = info.cpZf;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["hdQz"] = info.hdQz;
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["qzZf"] = info.qzZf;
        }

        for (var i in cepingHdglHdhzmxbList) {
          var info = cepingHdglHdhzmxbList[i];
          dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["zb_" + info.zbId] = info.zbFz;
        }
        let dataInfo = dataObj[(this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? "dept_" : "user_") + (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptId : row.userId)];
        dataInfo["hdName"] = cepingHdglb.hdName;

        let cppId = 0;
        for (var x in cepingHdglHdcphzbList) {
          let info = cepingHdglHdcphzbList[x];

          if (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子")) {
            if (info.bcpDeptId == row.deptId && info.bcpDx == "部门") {
              if (j == 1) {
               //cppId = info.cppId;
              cppId=cppIds[xi]

              }
            }
          } else {
            if (info.bcpUserId == row.userId && info.bcpDx == "个人") {
              if (j == 1) {
                //cppId = info.cppId;
                cppId=cppIds[xi]
              }
            }
          }
        }
        for (var x in cepingHdglCppmxbList) {
          let info = cepingHdglCppmxbList[x];
          if (info.cppId == cppId && j == 1) {
            let xmxxXmmx = info.xmxxXmmx;
            this.cepingHdglCppmxbInfo = info;
            this.xmxxXmmx = JSON.parse(xmxxXmmx);
            this.cepingXmmxbListTree = this.getTree(JSON.parse(xmxxXmmx), 0);
          }
        }
        tableData.push(dataInfo);
        if (j >= hdids.length) {
          let d = JSON.parse(JSON.stringify(tableData[0]));
          d.hdName = (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptName : row.userName);
          for (var i in tableData) {
            for (var k in tableData[i]) {
              if (k.indexOf("zb_") != -1) {
                if (d[k.replace("zb_", "js_")] == undefined) {
                  d[k.replace("zb_", "js_")] = tableData[i][k] * 1 * tableData[i].hdQz / 100;
                } else {
                  d[k.replace("zb_", "js_")] += tableData[i][k] * 1 * tableData[i].hdQz / 100;
                }
              }
            }
          }
          if (this.cepingHzglb.id < 69) {
            let hj = 0;
            for (var k in d) {
              if (k.indexOf("js_") != -1) {
                hj += d[k];
              }
            }
            let bl = row.df / hj;
            hj = 0;
            for (var k in d) {
              // if(k.indexOf("961") != -1)
              //   debugger
              if (k.indexOf("js_") != -1) {
                let db = d[k] * bl;
                let jg = Math.round(db * 100) / 100;
                d[k.replace("js_", "hj_")] = jg;
                d[k.replace("js_", "db_")] = db > jg ? -1 : (db < jg ? 1 : 0);
                hj += jg;

              }
            }
            hj = Math.round(hj * 100) / 100;

            if (hj != row.df) {
              let db = Math.round((hj - row.df) * 100) / 100;
              while (db != 0) {
                for (var k in d) {
                  if (k.indexOf("hj_") != -1) {
                    if (db > 0 && d[k.replace("hj_", "db_")] == 1) {
                      if (Math.round(d[k]) != d[k]) {
                        d[k] = Math.round((d[k] - 0.01) * 100) / 100;
                        db = Math.round((db - 0.01) * 100) / 100;
                      }
                    }
                    if (db < 0 && d[k.replace("hj_", "db_")] == -1) {
                      if (Math.round(d[k]) != d[k]) {
                        d[k] = Math.round((d[k] + 0.01) * 100) / 100;
                        db = Math.round((db + 0.01) * 100) / 100;
                      }
                    }
                    if (db == 0) {
                      break;
                    }
                  }
                }
              }
            }
          }

          d.cpFz = row.df;
          //console.log("d.cpFz221111,",d.cpFz)

          this.tableData.push(d);
          // if(a>=47){
          //
          //   debugger
          // }
          
        }
      }
      if (a >= this.cepingHzglDxpzbList.length - 1) {
        this.getXQ(0);
      } else {
        this.viewMxAlls(a + 1);
      }
      if(hdids.length == 0){
        if (a >= this.cepingHzglDxpzbList.length - 1) {
          this.getXQ(0);
        } else {
          this.viewMxAlls(a + 1);
        }
      }
    },
    async getXQ(a) {
      this.loading.text = '正在汇总计算详情，请销后...';
      var hdids = [];
      for (var i in this.cepingHzglHdfwbList) {
        if (hdids.indexOf(this.cepingHzglHdfwbList[i].hdId) == -1) {
          hdids.push(this.cepingHzglHdfwbList[i].hdId);
        }
      }
      var data1 = [];
      var data2 = [];
      for (var i in this.tableData) {
        if (this.tableData[i].bcpDx == "个人") {
          data2.push(this.tableData[i].bcpUserId)
        } else {
          data1.push(this.tableData[i].bcpDeptId)
        }
      }
      await getBcpDfLists(hdids.join(), data1.length == 0 ? "0" : data1.join(), data2.length == 0 ? "0" : data2.join()).then(res => {
        this.dfInfos = res.data.data;
        this.getXQs(a, hdids);
      })
    },
    getBcpDf(t, id) {
      let res = {
        data: {
          data: this.dfInfos[t][id]
        }
      }
      return res;
    },
    selectDf() {
      var existVar = [];
      var map = {}
      for (var x in this.xmxxXmmx) {
        map[this.xmxxXmmx[x].id] = 1;
      }
      for(var a in this.tableData){
        let res = this.getBcpDf(this.tableData[a].bcpDx == "个人" ? "user" : "dept", this.tableData[a].bcpDx == "个人" ? this.tableData[a].bcpUserId : this.tableData[a].bcpDeptId);
        let cepingHdglXpxxDfjlBfbList = res.data.data.cepingHdglXpxxDfjlBfbList;
        let cepingHdglXpxxbList = res.data.data.cepingHdglXpxxbList;
        let cepingHdglXpxxbs = {};
        for (var i in cepingHdglXpxxbList) {
          cepingHdglXpxxbs[cepingHdglXpxxbList[i].id] = cepingHdglXpxxbList[i];
        }

        for (var i in cepingHdglXpxxDfjlBfbList) {
          let info = cepingHdglXpxxDfjlBfbList[i];
            let xpCode = cepingHdglXpxxbs[info.xpId].xpCode;
            let xp = ""
            if (xpCode.length > 11) {
              xp = xpCode.substr(0, 2);
            } else {
              xp = xpCode.substr(0, 1);
            }
            //第一个if
            var key = a+"_"+xp+"_"+info.zbId+"_df";
            if(!existVar[key]){
             existVar[key] = 1;
             this.tableData[a][xp+"_"+info.zbId+"_df"] = 0;
            }
            //第二个if
            var key1 = a+"_"+xp+"_dfl";
            if(!existVar[key1]){
             existVar[key1] = 1;
             this.tableData[a][xp+"_dfl"] = 0;
            }
            // if(!this.tableData[a][xp + '_' + info.zbId + '_df'] || this.tableData[a][xp + '_' + info.zbId + '_df']==0){

            //   this.tableData[a][xp + '_' + info.zbId + '_df'] = 0;
            // }
            // if(xp == 'B3' && info.zbId==797){
            //   console.log(info)
            //     console.log(info.fzFz  + "  "+ this.xyfz)
            // }
            // console.log(info.fzFz  + "  "+ this.xyfz)
            
            if (info.fzFz < this.xyfz) {
              // if(xp == 'B3' && info.zbId==797){
              //   console.log("zbId："+ xp + '_' + info.zbId + '_df')
              // }
              this.tableData[a][xp + '_' + info.zbId + '_df']++;
              // if(xp == 'B3' && info.zbId==797){
              //   console.log("tableData"+ this.tableData[a][xp + '_' + info.zbId + '_df'])
              // }
              if(map[info.zbId]){
                  if(this.tableData[a][xp+'_dfl']){
                    this.tableData[a][xp+'_dfl']++;
                  }else{
                    this.tableData[a][xp+'_dfl']=1;
                  }
              }
              
            }
        }
      }
    },
    getXQs(a, hdids) {
      this.loading.text = '正在汇总计算详情（' + (a + 1) + '/' + (this.tableData.length) + '），请销后...';
      let res = this.getBcpDf(this.tableData[a].bcpDx == "个人" ? "user" : "dept", this.tableData[a].bcpDx == "个人" ? this.tableData[a].bcpUserId : this.tableData[a].bcpDeptId);
      let cepingHdglXpxxDfjlBfbList = res.data.data.cepingHdglXpxxDfjlBfbList;
      let cepingHdglXpxxbList = res.data.data.cepingHdglXpxxbList;
      let cepingHdglXpxxbs = {};
      for (var i in cepingHdglXpxxbList) {
        cepingHdglXpxxbs[cepingHdglXpxxbList[i].id] = cepingHdglXpxxbList[i];
      }
// if(a===0){
  // console.log('this.tableData[a]:'+JSON.stringify(this.tableData[a]))
  // console.log('cepingHdglXpxxDfjlBfbList:'+JSON.stringify(cepingHdglXpxxDfjlBfbList[a]))
  //获取zbId数组去重
  let zbIdList = Array.from(new Set(cepingHdglXpxxDfjlBfbList.map(item => item.zbId)));
  // console.log('zbIdList:'+JSON.stringify(zbIdList))
  //获取测评票明细ID数组 去重
  var cppmxIdList = Array.from(new Set(cepingHdglXpxxDfjlBfbList.map(item => item.cpmxId)));
  // console.log('cppmxIdList:'+JSON.stringify(cppmxIdList))
  //获取测评活动ID数组 去重
  var hdIdList = Array.from(new Set(cepingHdglXpxxDfjlBfbList.map(item => item.hdId)));
  // console.log('cppmxIdList:'+JSON.stringify(hdIdList))
  let cepingHdglXpxxDfjlBfbListForPj = []
  var bcpUserId = this.tableData[a].bcpUserId
  var bcpDeptId = this.tableData[a].bcpDeptId
  var bcpDx = this.tableData[a].bcpDx 
  var hdId = this.tableData[a].hdId
  zbIdList.forEach(function(zbid,index) {
    // console.log('zbid:'+JSON.stringify(zbid))
    hdIdList.forEach(function(hdId,index){
      // console.log('cppmxId:'+JSON.stringify(cppmxId))
      //通过zbid和cppmxid查询bfb明细
      var cepingHdglXpxxDfjlBfbListTemp = cepingHdglXpxxDfjlBfbList.filter(item =>  {
        // console.log('cepingHdglXpxxbs[item.xpId]:'+JSON.stringify(cepingHdglXpxxbs[item.xpId]))
        if(bcpDx === "部门"){
          if(item.zbId === zbid && item.hdId === hdId && cepingHdglXpxxbs[item.xpId] && bcpDeptId === item.bcpDeptId){
            return item
          }
        }else{
          if(item.zbId === zbid && item.hdId === hdId && cepingHdglXpxxbs[item.xpId] && bcpUserId === item.bcpUserId){
            return item
          }
        }
        })
      if(cepingHdglXpxxDfjlBfbListTemp.length>0){
        // console.log('cepingHdglXpxxDfjlBfbListTemp:'+JSON.stringify(cepingHdglXpxxDfjlBfbListTemp))
        var fzFzpj = 0
        var qzFzpj = 0
        cepingHdglXpxxDfjlBfbListTemp.forEach(function(item) {
          fzFzpj += item.fzFz
          qzFzpj += item.qzFz
        })

        let xpCode = cepingHdglXpxxbs[cepingHdglXpxxDfjlBfbListTemp[0].xpId].xpCode;
        let xp = ""
        if (xpCode.length > 11) {
          xp = xpCode.substr(0, 2);
        } else {
          xp = xpCode.substr(0, 1);
        }
  
        var bfb = {
          zbId: zbid,
          xp: xp,
          hdId: hdId,
          zbQz: cepingHdglXpxxDfjlBfbListTemp[0].zbQz,
          fzFz: fzFzpj/cepingHdglXpxxDfjlBfbListTemp.length ,
          qzFz: qzFzpj/cepingHdglXpxxDfjlBfbListTemp.length ,
        }

        cepingHdglXpxxDfjlBfbListForPj.push(bfb)
      }
      
    })
  })
      // console.log('cepingHdglXpxxDfjlBfbListForPj:'+JSON.stringify(cepingHdglXpxxDfjlBfbListForPj))
  for (var i in cepingHdglXpxxDfjlBfbListForPj) {
    let info = cepingHdglXpxxDfjlBfbListForPj[i];
    // console.log('info:'+JSON.stringify(info))
    // if(cepingHdglXpxxbs[info.xpId]){
    //     }else{
    //         continue;
    // }
    // let xpCode = cepingHdglXpxxbs[info.xpId].xpCode;
    let xp = info.xp
    // if (xpCode.length > 11) {
    //   xp = xpCode.substr(0, 2);
    // } else {
    //   xp = xpCode.substr(0, 1);
    // }
      // if(a===0 &&this.tableData[a].bcpUserId===425 && xp==='C'&& info.zbId === 978){
      //   console.log('infoMx:'+info.hdId +':'+ info.qzFz+':'+ info.fzFz+':'+ info.xp)
      //   // console.log('infoMx:'+JSON.stringify(info))
      // }
    if (!this.tableData[a][info.zbId + '_info']) {
      this.tableData[a][info.zbId + '_info'] = {};
    }
    if (!this.tableData[a][xp + '_' + info.zbId + '_xp']) {
      this.tableData[a][xp + '_' + info.zbId + '_xp'] = 0;
      this.tableData[a][xp + '_' + info.zbId + '_df'] = 0;
      this.tableData[a][xp + '_' + info.zbId + '_zf'] = 0;
      this.tableData[a][xp + '_' + info.zbId + '_pj'] = 0;
      this.tableData[a][xp + '_' + info.zbId + '_sl'] = 0;
    }
    this.tableData[a][xp + '_' + info.zbId + '_zf'] += info.fzFz;
    this.tableData[a][xp + '_' + info.zbId + '_xp']++;
    if (info.fzFz < this.xyfz) {
      this.tableData[a][xp + '_' + info.zbId + '_df']++;
    }
    this.tableData[a][xp + '_' + info.zbId + '_qz'] = info.zbQz;
var zbid = info.zbId
// if(a == 0 && xp == 'B3' && zbid == 797){
//   console.log("info.fzFz:"+info.fzFz)
//   console.log("info.zbQz:"+info.zbQz)
//   console.log("a:"+a)
// }

    this.tableData[a][xp + '_' + info.zbId + '_pj'] = Math.round(this.tableData[a][xp + '_' + info.zbId + '_zf'] / this.tableData[a][xp + '_' + info.zbId + '_xp'] * 100 * info.zbQz / 100) / 100;
    // this.tableData[a][xp + '_' + info.zbId + '_pj'] = Math.round(this.tableData[a][xp + '_' + info.zbId + '_zf'] / this.tableData[a][xp + '_' + info.zbId + '_xp'] * 100 * info.zbQz / 100) / 100;

      // if( this.tableData[a].bcpUserId===425 && xp==='C' && info.zbId === 978 ){
      // // if(xp === 'C' && info.zbId === 978 && a===0&& this.tableData[a].bcpUserId===425){
      //   // console.log('info:'+JSON.stringify(info.qzFz));
      //   // console.log(' this.tableData[a][_zf +]:'+ this.tableData[a][xp + '_' + info.zbId + '_zf']);
      //   // console.log(' this.tableData[a][_xp +]:'+ this.tableData[a][xp + '_' + info.zbId + '_xp']);
      //   // console.log(' this.tableData[a][_pj +]:'+ this.tableData[a][xp + '_' + info.zbId + '_pj']);
      // }

    this.tableData[a][info.zbId + '_info'][xp] = this.tableData[a][xp + '_' + info.zbId + '_pj'] * 1;
  }
// }else{

//   for (var i in cepingHdglXpxxDfjlBfbList) {
//     let info = cepingHdglXpxxDfjlBfbList[i];
//     if(cepingHdglXpxxbs[info.xpId]){
//       }else{
//         continue;
//     }
//     let xpCode = cepingHdglXpxxbs[info.xpId].xpCode;
//     let xp = ""
//     if (xpCode.length > 11) {
//       xp = xpCode.substr(0, 2);
//     } else {
//       xp = xpCode.substr(0, 1);
//     }
//       // if(a===0 &&this.tableData[a].bcpUserId===425 && xp==='C'&& info.zbId === 978){
//       //   // console.log('infoMx:'+info.cpmxId +':'+ info.qzFz)
//       //   console.log('infoMx:'+JSON.stringify(info))
//       // }
//     if (!this.tableData[a][info.zbId + '_info']) {
//       this.tableData[a][info.zbId + '_info'] = {};
//     }
//     if (!this.tableData[a][xp + '_' + info.zbId + '_xp']) {
//       this.tableData[a][xp + '_' + info.zbId + '_xp'] = 0;
//       this.tableData[a][xp + '_' + info.zbId + '_df'] = 0;
//       this.tableData[a][xp + '_' + info.zbId + '_zf'] = 0;
//       this.tableData[a][xp + '_' + info.zbId + '_pj'] = 0;
//       this.tableData[a][xp + '_' + info.zbId + '_sl'] = 0;
//     }
//     this.tableData[a][xp + '_' + info.zbId + '_zf'] += info.fzFz;
//     this.tableData[a][xp + '_' + info.zbId + '_xp']++;
//     if (info.fzFz < this.xyfz) {
//       this.tableData[a][xp + '_' + info.zbId + '_df']++;
//     }
//     this.tableData[a][xp + '_' + info.zbId + '_qz'] = info.zbQz;
//     this.tableData[a][xp + '_' + info.zbId + '_pj'] = Math.round(this.tableData[a][xp + '_' + info.zbId + '_zf'] / this.tableData[a][xp + '_' + info.zbId + '_xp'] * 100 * info.zbQz / 100) / 100;

//       // if( this.tableData[a].bcpUserId===425 && xp==='C' && info.zbId === 978 ){
//       // // if(xp === 'C' && info.zbId === 978 && a===0&& this.tableData[a].bcpUserId===425){
//       //   // console.log('info:'+JSON.stringify(info.qzFz));
//       //   // console.log(' this.tableData[a][_zf +]:'+ this.tableData[a][xp + '_' + info.zbId + '_zf']);
//       //   // console.log(' this.tableData[a][_xp +]:'+ this.tableData[a][xp + '_' + info.zbId + '_xp']);
//       //   // console.log(' this.tableData[a][_pj +]:'+ this.tableData[a][xp + '_' + info.zbId + '_pj']);
//       // }

//     this.tableData[a][info.zbId + '_info'][xp] = this.tableData[a][xp + '_' + info.zbId + '_pj'] * 1;
//   }
// }
      //原值69，不知道什么意义
      // if (this.cepingHzglb.id < 69) {
      if (this.cepingHzglb.id < 0) {

      } else {
        //console.log(this.tableData[a])
        ////console.log(this.cepingHzglDxpzbList[a])
        let hj = 0;
         var gs = "";
        for (var i in this.tableData[a]) {
          var row = this.tableData[a];
          if (i.indexOf("_info") != -1) {
            var info = row[i];
            var zbId = i.replace("_info", "");


            if (this.cepingHzglb.hzFs == "公式") {
              gs = this.cepingHzglb.hzJsgs;
            }

            var x = 0,cppQz,cpQz,qz;
            for (var j in info) {
              if(this.cepingHzglDxpzbList[a][j + "_cppQz"]&&this.cepingHzglDxpzbList[a][j + "_cpQz"]){
                  cppQz = this.cepingHzglDxpzbList[a][j + "_cppQz"] * 1;
                   cpQz = this.cepingHzglDxpzbList[a][j + "_cpQz"] * 1;
                   qz = cppQz * cpQz / 100 / 100;
                    if (this.cepingHzglb.hzFs == "合计") {
                    gs += (gs == "" ? "" : "+") + (info[j] * qz);
                  } else if (this.cepingHzglb.hzFs == "平均") {
                    gs += (gs == "" ? "" : "+") + (info[j] * qz);
                  } else {
                    gs = gs.replace(j, info[j] * qz);
                  }
              }

              x++;
            }
            if (this.cepingHzglb.hzFs == "平均") {
              gs = "(" + gs + ")/" + x;
            }
           //console.log()
            //票中无值的替换成指定值 1
            gs = gs.replace(/[A-Z][0-9]?/g,'1')
            var db = eval(gs);
            // var jg = Math.round(db * 100) / 100;
            var jg = this.keepTwoDecimalFull(db,4);
            this.tableData[a]['hjy_' + zbId] = jg;
            this.tableData[a]['hj_' + zbId] = jg;
            this.tableData[a]['db_' + zbId] = db > jg ? -1 : (db < jg ? 1 : 0);
            hj += jg;
          }
        }

        ////console.log(this.tableData[a])
        // hj = Math.round(hj * 100) / 100;
       ////console.log("hqqian"+hj)
        hj = this.keepTwoDecimalFull(hj,4);
        //console.log("hq后"+hj)
        var row1 = this.tableData[a];
        var d = this.tableData[a];
        if (hj != row1.cpFz) {
          let db = Math.round((hj - row1.cpFz) * 100) / 100;
          let q = 0;
          while (db != 0) {
            for (var k in d) {
              q++;
              if (k.indexOf("hj_") != -1) {
                if (db > 0 && d[k.replace("hj_", "db_")] == 1) {
                  if (Math.round(d[k]) != d[k]) {
                    d[k] = Math.round((d[k] - 0.01) * 100) / 100;
                    db = Math.round((db - 0.01) * 100) / 100;
                  }
                }
                if (db < 0 && d[k.replace("hj_", "db_")] == -1) {
                  if (Math.round(d[k]) != d[k]) {
                    d[k] = Math.round((d[k] + 0.01) * 100) / 100;
                    db = Math.round((db + 0.01) * 100) / 100;
                  }
                }
                if (db == 0) {
                  break;
                }
              }
            }
            if (q > 1000) {
              break;
            }
          }
        }
      }


      if (a >= this.tableData.length - 1) {
        this.loading.close();
        this.xqDialogVisible = true;
        let t = this.mxLx + "";
        this.mxLx = "";
        this.$nextTick(() => {
          this.mxLx = t;
        })
      } else {
        this.getXQs(a + 1, hdids);
      }
//console.log("走着了么:"+JSON.stringify(this.tableData))
    },
    keepTwoDecimalFull(num, p) {
      let result = parseFloat(num);
      if (isNaN(result)) {
       //console.log('传递参数错误，请检查！');
        return num;
      }
      let multiplicator = Math.pow(10, p);
      result = Math.round(num * multiplicator) / multiplicator;
      let s_x = result.toString();
      let pos_decimal = s_x.indexOf('.');
      if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
      }
      while (s_x.length <= pos_decimal + p) {
        s_x += '0';
      }
      return s_x;
    },
    getDfsm(row) {
      let text = "";
      var map = {};
      for (var x in this.xmxxXmmx) {
        map[this.xmxxXmmx[x].id] = 1;
      }
      for (var i in this.tableData) {
        let ys = false;
        if (row.userLx == "个人") {
          if (this.tableData[i].bcpUserId == row.userId) {
            ys = true;
          }
        } else {
          if (this.tableData[i].bcpDeptId == row.deptId) {
            ys = true;
          }
        }
        if (ys) {
          var info = this.tableData[i];
          //console.log(info);
          var jg = {}
          for (let j in info) {
            if (j.indexOf("_df") != -1 && j.indexOf("_dfl") == -1 ) {
              if (info[j] > 0) {
                
                let xp = j.split("_")[0];
                let zb = j.split("_")[1];
                if(map[zb]){
                  let df = info[j];
                  if (!jg[zb]) {
                    jg[zb] = {}
                  }
                  jg[zb][xp] = df;
                }
                
              }
            }
          }
          var y = 1;
          for (var j in jg) {
            let name = "";
            for (var x in this.xmxxXmmx) {
              if (this.xmxxXmmx[x].id == j * 1) {
                name = this.xmxxXmmx[x].zbMc;
              }
            }
            text += y + "、" + name + "：";
            y++;
            for (var s in jg[j]) {
              text += s + " " + jg[j][s] + "票；";
            }
            text += "<br>";
          }
        }
      }
      return text;
    },
    viewMx(row) {
      this.loading = this.$loading({
        lock: true,
        text: '正在汇总计算，请销后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let tableData = [];
      let hdids = [];
      for (var i in this.cepingHzglHdfwbList) {
        if (row["hd_" + this.cepingHzglHdfwbList[i].hdId]) {
          hdids.push(this.cepingHzglHdfwbList[i].hdId)
        }
      }
      let j = 0;
      for (let i in hdids) {
        setTimeout(() => {
          hdhz(hdids[i]).then(res => {
            j++;
            let cepingHdglb = res.data.data.cepingHdglb;
            let cepingHdglCppmxbList = res.data.data.cepingHdglCppmxbList;
            let cepingHdglHdcphzbList = res.data.data.cepingHdglHdcphzbList;
            //this.cepingXmmxbList = res.data.data.cepingXmmxbList;
            let cepingHdglHdzhhzbList = res.data.data.cepingHdglHdzhhzbList;
            let cepingHdglHdhzmxbList = res.data.data.cepingHdglHdhzmxbList;

            //填表
            let dataObj = {};
            let data = cepingHdglHdzhhzbList;
            for (var i in data) {
              dataObj[(data[i].bcpDx == '部门' ? "dept" : "user") + "_" + (data[i].bcpDx == '部门' ? data[i].bcpDeptId : data[i].bcpUserId)] = data[i];
            }

            for (var i in cepingHdglHdcphzbList) {
              var info = cepingHdglHdcphzbList[i];
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpFz"] = info.cpFz;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpDj"] = info.cpDj;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["cpZf"] = info.cpZf;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["hdQz"] = info.hdQz;
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["qzZf"] = info.qzZf;
            }

            for (var i in cepingHdglHdhzmxbList) {
              var info = cepingHdglHdhzmxbList[i];
              dataObj[(info.bcpDx == '部门' ? "dept" : "user") + "_" + (info.bcpDx == '部门' ? info.bcpDeptId : info.bcpUserId)]["zb_" + info.zbId] = info.zbFz;
            }
            let dataInfo = dataObj[(this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? "dept_" : "user_") + (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptId : row.userId)];
            dataInfo["hdName"] = cepingHdglb.hdName;

            let cppId = 0;
            for (var x in cepingHdglHdcphzbList) {
              let info = cepingHdglHdcphzbList[x];
              if (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子")) {
                if (info.bcpDeptId == row.deptId && info.bcpDx == "部门") {
                  if (j == 1) {
                    cppId = info.cppId;
                  }
                }
              } else {
                if (info.bcpUserId == row.userId && info.bcpDx == "个人") {
                  if (j == 1) {
                    cppId = info.cppId;
                  }
                }
              }
            }
            for (var x in cepingHdglCppmxbList) {
              let info = cepingHdglCppmxbList[x];
              if (info.cppId == cppId && j == 1) {
                let xmxxXmmx = info.xmxxXmmx;

                this.cepingHdglCppmxbInfo = info;
                this.cepingXmmxbListTree = this.getTree(JSON.parse(xmxxXmmx), 0);
              }
            }
            // debugger
            tableData.push(dataInfo);
            //console.log(tableData)
            if (j >= hdids.length) {
              let d = JSON.parse(JSON.stringify(tableData[0]));
              d.hdName = (this.cepingHzglb.hzDx == "部门" || (this.cepingHzglb.hzDx == "个人" && row.bcpDx == "班子") ? row.deptName : row.userName);

              for (var i in tableData) {
                for (var k in tableData[i]) {
                  if (k.indexOf("zb_") != -1) {
                    if (d[k.replace("zb_", "js_")] == undefined) {
                      d[k.replace("zb_", "js_")] = tableData[i][k] * 1 * tableData[i].hdQz / 100;
                    } else {
                      d[k.replace("zb_", "js_")] += tableData[i][k] * 1 * tableData[i].hdQz / 100;
                    }
                  }
                }
              }

              let hj = 0;
              for (var k in d) {
                if (k.indexOf("js_") != -1) {
                  hj += d[k];
                }
              }
              //console.log(d)
              let bl = row.df / hj;
              hj = 0;
              for (var k in d) {
                if (k.indexOf("js_") != -1) {
                  let db = d[k] * bl;
                  let jg = Math.round(db * 100) / 100;
                  d[k.replace("js_", "hj_")] = jg;
                  d[k.replace("js_", "db_")] = db > jg ? -1 : (db < jg ? 1 : 0);
                  hj += jg;
                }
              }
              hj = Math.round(hj * 100) / 100;
              //console.log(d)
              if (hj != row.df) {
                let db = Math.round((hj - row.df) * 100) / 100;
                while (db != 0) {
                  for (var k in d) {
                    if (k.indexOf("hj_") != -1) {
                      if (db > 0 && d[k.replace("hj_", "db_")] == 1) {
                        if (Math.round(d[k]) != d[k]) {
                          d[k] = Math.round((d[k] - 0.01) * 100) / 100;
                          db = Math.round((db - 0.01) * 100) / 100;
                          if (db <= 0) {
                            break;
                          }
                        }
                      }
                      if (db < 0 && d[k.replace("hj_", "db_")] == -1) {
                        if (Math.round(d[k]) != d[k]) {
                          d[k] = Math.round((d[k] + 0.01) * 100) / 100;
                          db = Math.round((db + 0.01) * 100) / 100;
                          if (db >= 0) {
                            break;
                          }
                        }
                      }

                    }
                  }
                }
              }
               d.cpFz = row.df;
              //console.log("d.cpFz----------------------------------------,",d.cpFz)
              this.tableData = [];
              this.tableData.push(d);
              this.getXQ(0, hdids);
            }
          })
        }, i * 100)
      }
    },
    getDjmx() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cpqz != 100) {
          return true;
        }
      }
      return r;
    },
    getCppQz() {
      let r = false;
      for (var i in this.mxList) {
        if (this.mxList[i].cppQz != 100) {
          return true;
        }
      }
      return r;
    },
    getTree(ary, pid) {
      return ary.filter(v => {
        if (v.pid === pid) {
          if (v.pid === 0) {
            v.xh = 1;
          } else {
            for (var a = 0; a < ary.length; a++) {
              if (ary[a].id == v.pid) {
                v.xh = ary[a].xh + 1;
              }
            }
          }
          return v
        }
      }).map(v => {
        const children = this.getTree(ary, v.id);
        if (children.length) v.children = this.getTree(ary, v.id);
        return v
      });
    },
    exportExcelAB() {
      const excelName = this.cepingHzglb.hzName + "_明细"
      try {
        const $e = this.$refs['zbABTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    },
    exportExcelXq() {
      const excelName = this.cepingHzglb.hzName + "_详情"
      try {
        const $e = this.$refs['xqABTable'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if (!$table) {
          $table = $e
        }
        const wb = XLSX.utils.table_to_book($table, {
          raw: true
        })

        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array'
        })
        FileSaver.saveAs(
          new Blob([wbout], {
            type: 'application/octet-stream'
          }),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  }
}
</script>

<style scoped>
</style>
